/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.biz.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.modules.biz.domain.Settings;
import me.zhengjie.modules.biz.service.SettingsService;
import me.zhengjie.modules.biz.service.dto.StorehousrConfigQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://eladmin.vip
* @author Admin
* @date 2023-05-04
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "setting管理")
@RequestMapping("/api/storehousrConfig")
public class SettingsController {

    private final SettingsService settingsService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
//    @PreAuthorize("@el.check('storehousrConfig:list')")
    public void exportStorehousrConfig(HttpServletResponse response, StorehousrConfigQueryCriteria criteria) throws IOException {
        settingsService.download(settingsService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询setting")
    @ApiOperation("查询setting")
//    @PreAuthorize("@el.check('storehousrConfig:list')")
    public ResponseEntity<Object> queryStorehousrConfig(StorehousrConfigQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(settingsService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增setting")
    @ApiOperation("新增setting")
//    @PreAuthorize("@el.check('storehousrConfig:add')")
    public ResponseEntity<Object> createStorehousrConfig(@Validated @RequestBody Settings resources){
        return new ResponseEntity<>(settingsService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改setting")
    @ApiOperation("修改setting")
//    @PreAuthorize("@el.check('storehousrConfig:edit')")
    public ResponseEntity<Object> updateStorehousrConfig(@Validated @RequestBody Settings resources){
        settingsService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @PostMapping("/updateAll")
    @Log("批量修改setting")
    @ApiOperation("批量修改setting")
    public ResponseEntity<Object> updateStorehousrConfigAll(@RequestBody Map<String, Object> resources){
        settingsService.updateAll(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除setting")
    @ApiOperation("删除setting")
//    @PreAuthorize("@el.check('storehousrConfig:del')")
    public ResponseEntity<Object> deleteStorehousrConfig(@RequestBody Integer[] ids) {
        settingsService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping("/queryByKeys")
    @Log("查询setting")
    @ApiOperation("查询setting")
//    @PreAuthorize("@el.check('storehousrConfig:list')")
    public ResponseEntity<Object> queryByKeys(StorehousrConfigQueryCriteria criteria){
        return new ResponseEntity<>(settingsService.queryByKeys(criteria),HttpStatus.OK);
    }
}
